
if (!window.DataGrid) {
	var DataGrid = function () {
		var OFFSET = 4;
        // private variables
		var columnNames, totalRows, pageSize = 10, table, TableId;
		var classNames, selection, pkName, nodeName, columns;
		var showFooter = false;
		var totalPages;
		var operations = [];
		var inputNo = null, inputSize = null;
		var handler, selectionHandler;
		var createRowhandler, isMycreateRow = false;
		var notClearData = false;

        // "public" functions
		this.setNotClearData = function () {
			notClearData = true;
		};
		this.setCreateRowhandler = function (value) {
			createRowhandler = value;
			isMycreateRow = true;
		};
		this.setColumnNames = function (names) {
			columnNames = names;
		};
		this.setTotalRows = function (rows) {
			totalRows = rows;
		};
		this.getTotalRows = function () {
			return totalRows;
		};
		this.setPageSize = function (size) {
			pageSize = size;
		};
		this.addOperation = function (oper) {
			operations.push(oper);
		};
		this.setNodeName = function (name) {
			nodeName = name;
		};
		this.setShowFooter = function (value) {
			showFooter = value;
		};
		this.setPKName = function (names) {
			pkName = names;
		};
		this.setHandler = function (value) {
			handler = value;
		};
		this.setPKHandler = function (value) {
			selectionHandler = value;
		};
		this.setControl = function (value) {
			columns = value;
		};
		this.getTotalPages = function () {
			return totalPages;
		};
		this.setSelection = function (mode) {
			switch (mode) {
			  case "one":
				selection = "radio";
				break;
			  case "many":
				selection = "checkbox";
				break;
			  default:
				selection = false;
			}
		};
		this.setInputForPageNo = function (pageNo) {
			inputNo = pageNo;
		};
		this.setInputForPageSize = function (pageSize) {
			inputSize = pageSize;
		};
		this.setTable = function (tableId) {
			TableId = tableId;
			table = document.getElementById(tableId);
			if (!table) {
				throw "Can't find table width id : " + tableId;
			}
		};
		this.getTableId = function () {
			return TableId;
		};
		this.setClassNames = function (classes) {
			if (!(classes instanceof Array)) {
				throw "classes must be an array!";
			}
			classNames = classes;
			if (classNames.length == 0) {
				classNames.push(classNames[0]);
			}
		};
		this.setData = function (list) {
			if (!notClearData) {
				clearData();
			}
			var meta = list.selectSingleNode("//meta");
			var data = list.selectNodes("//" + nodeName);
			this.pageRows = data.length;
			for (var i = 0; i < data.length; i++) {
				var row = table.insertRow(-1);
				var cell;
				if (selection) {
					cell = row.insertCell(-1);
					cell.className = classNames[i % 2];
					cell.appendChild(createSelection(data[i]));
				}
				if (isMycreateRow) {
					createRowhandler(data[i], row, i);
				} else {
					createRow(data[i], row, i);
				}
				if (operations.length != 0) {
					createOperationColumn(data[i], row, i);
				}
			}
			if (meta) {
				buildFooter(meta);
			}
		};
		this.clear = function (){clearData();}
        // "private" functions
		function clearData() {
			while (table.rows.length > 1) {
				table.deleteRow(1);
			}
		}
		function createSelection(datarow) {
			var box = document.createElement("input");
			box.type = selection;
			if (!(pkName instanceof Array)) {
				if (!pkName) {
					pkName = columnNames[0];
				}
				box.name = pkName;
				box.id = pkName;
				box.value = datarow.getAttribute(pkName);
			} else {
				box.name = pkName[0];
				box.id = pkName[0];
				var ids = "";
				for (var index = 1; index < pkName.length; index++) {
					if (index == pkName.length - 1) {
						ids += datarow.getAttribute(pkName[index]);
					} else {
						ids += datarow.getAttribute(pkName[index]) + ",";
					}
				}
				box.value = ids;
			}
			if (selectionHandler) {
				box.onclick = selectionHandler;
			}
			return box;
		}
		function createRow(datarow, row, index) {
			for (var i = 0; i < columnNames.length; i++) {
				var cell = row.insertCell(-1);
				if (classNames && classNames.length >= 2) {
					cell.className = classNames[index % 2];
				} else {
					cell.style.backgroundColor = "white";
				}
				var attrName = columnNames[i];
				var html = attrName ? datarow.getAttribute(attrName) : "";
				html = html ? html : "";
				cell.innerHTML = html;
				cell.title = html;
			}
		}
		function createOperationColumn(datarow, row, index) {
			var cell = row.insertCell(-1);
			cell.align = "center";
			cell.className = classNames[index % 2];
			for (var i = 0; i < operations.length; i++) {
				if (i != 0) {
					cell.appendChild(document.createTextNode(" | "));
				}
				var oper = operations[i];
				var link = document.createElement("a");
				var pattern = "xxxxx";
				if (oper.urlFormat) 
				{
					link.href = ParsePattern (oper.urlFormat, datarow);
					link.innerHTML = oper.caption;
				}
				else if (oper.onClick) 
				{
					link.href = "#";
					pattern = ParsePattern (oper.onClick, datarow);
					link.innerHTML = "<span onclick = \"" + pattern + "\">" + oper.caption + "</span>";
				}
				
				cell.appendChild (link);
/*				
				if (oper.urlFormat)
				{
					var pattern = ParsePattern (oper.urlFormat, datarow);
					link.href = pattern;
					link.innerHTML = oper.caption;
					cell.align = "center";
					cell.appendChild(link);				
				}
				else if (oper.onClick)
				{
					var span = document.createElement("span");
					var pattern = ParsePattern (oper.onClick, datarow);
					span.innerHTML = "<span onclick = \"" + pattern + "\">" + oper.caption + "</span>";
					span.style.cursor = "pointer";
					cell.align = "center";
					cell.appendChild (span);
				}
*/
			}
		}
		this.makeFooter = function (pageno, pagesize, totalpages, totalrows) {
			var Em = makeMeta(pageno, pagesize, totalpages, totalrows);
			buildFooter(Em);
		};
        //<meta page-no="1" page-size="10" total-pages="1" total-rows="5" />
        //used by RouteUpdate.js
		function makeMeta(pageno, pagesize, totalpages, totalrows) {
			var Emeta = document.createElement("meta");
			Emeta.setAttribute("page-no", pageno);
			Emeta.setAttribute("page-size", pagesize);
			Emeta.setAttribute("total-pages", totalpages);
			Emeta.setAttribute("total-rows", totalrows);
			return Emeta;
		}
		function click_handler(e) {
			if (!e) {
				e = window.event;
			}
			if (!e.target) {
				e.target = e.srcElement;
			}
			var PageSize = document.getElementById(inputSize);
			if (!PageSize) {
				PatchPage();
			}
			PageSize = document.getElementById(inputSize);
			PageSize.value = pageSize;
			var PageNo = document.getElementById(inputNo);
			var value = e.target.innerHTML;
            var reg = /\[(\d+)\]/;
            value = value.replace (reg, "$1").trim ();
			PageNo.value = value;
			handler();
		}
		function buildFooter(meta) {
			totalRows = meta.getAttribute("total-rows");
			totalPages = meta.getAttribute("total-pages");
			var pageSize = parseInt(meta.getAttribute("page-size"), 10);
			var pageNo = parseInt(meta.getAttribute("page-no"), 10);
			if (!showFooter && totalRows <= pageSize) {
				return;
			}
			var row = table.insertRow(-1);
			var cell = row.insertCell(-1);
			row.bgColor = "white";
			var cols = columnNames.length;
			if (operations.length > 0) {
				cols++;
			}
			if (selection) {
				cols++;
			}
			cell.colSpan = cols;
			var t = document.createElement("table");
			t.style.fontSize = "12px";
			cell.appendChild(t);
			t.cellSpacing = 0;
			t.cellPadding = 0;
			t.width = "100%";
			var r = t.insertRow(-1);
			var c = r.insertCell(-1);
			c.align = "left";
			c.innerHTML = "\u5171" + totalRows + "\u884c \u7b2c" + pageNo + "/" + totalPages + "\u9875 \u6bcf\u9875" + pageSize + "\u884c";
			c = r.insertCell(-1);
			c.align = "right";
			var start = pageNo - OFFSET;
			if (start <= 0) {
				start = 1;
			}
			var end = pageNo + OFFSET;
			if (end > totalPages) {
				end = totalPages;
			}
			if (pageNo != start) {
				var left = document.createElement("img");
				left.src = "images/left.gif";
				left.style.cursor = "pointer";
				left.title = "\u4e0a\u4e00\u9875";
				left.onclick = function () {
					var PageSize = document.getElementById(inputSize);
					if (!PageSize) {
						PatchPage();
					}
					PageSize = document.getElementById(inputSize);
					PageSize.value = pageSize;
					var PageNo = document.getElementById(inputNo);
					PageNo.value = pageNo - 1;
					handler();
				};
				c.appendChild(left);
				c.appendChild(document.createTextNode(" "));
				if (start > 1) {
					var first = document.createElement("span");
					first.innerHTML = "[1]";
					first.title = "\u7b2c1\u9875";
					first.style.cursor = "pointer";
					first.onclick = function (e) {
						click_handler(e);
					};
					c.appendChild(first);
					c.appendChild(document.createTextNode(" .. "));
				}
			}
			for (var i = start; i <= end; i++) {
				var span = document.createElement("span");
				span.innerHTML = "[" + i + "] ";
				span.title = "\u7b2c" + i + "\u9875";
				if (i != pageNo) {
					span.style.cursor = "pointer";
					span.onclick = function (e) {
						click_handler(e);
					};
				} else {
					span.style.fontWeight = "bold";
				}
				c.appendChild(span);
				c.appendChild(document.createTextNode(" "));
			}
			if (pageNo != end) {
				if (end < totalPages) {
					c.appendChild(document.createTextNode(" .. "));
					var last = document.createElement("span");
					last.innerHTML = "[" + totalPages + "]";
					last.title = "\u7b2c" + totalPages + "\u9875";
					last.style.cursor = "pointer";
					last.onclick = function (e) {
						click_handler(e);
					};
					c.appendChild(last);
				}
				var right = document.createElement("img");
				right.src = "images/right.gif";
				right.style.cursor = "pointer";
				right.title = "\u4e0b\u4e00\u9875";
				right.onclick = function () {
					var PageSize = document.getElementById(inputSize);
					if (!PageSize) {
						PatchPage();
					}
					PageSize = document.getElementById(inputSize);
					PageSize.value = pageSize;
					var PageNo = document.getElementById(inputNo);
					PageNo.value = pageNo + 1;
					handler();
				};
				c.appendChild(right);
			}
		}
		function PatchPage() {
			if (!inputNo || !inputSize) {
				inputNo = "PageNo";
				inputSize = "PageSize";
			}
			if (!document.getElementById(inputNo)) {
				var pageNo = document.createElement("input");
				pageNo.id = inputNo;
				pageNo.type = "hidden";
				document.body.appendChild(pageNo);
			}
			if (!document.getElementById(inputSize)) {
				var pageSize = document.createElement("input");
				pageSize.id = inputSize;
				pageSize.type = "hidden";
				document.body.appendChild(pageSize);
			}
		}
		
		function ParsePattern (pattern, datarow)
		{
			var reg = /%.*?%/g;
			var array = pattern.match(reg);
			if (array) {
				for (var j = 0; j < array.length; j++) {
					var attrName = array[j].substring(1, array[j].length - 1);
					var text = datarow.getAttribute(attrName);
					text = text ? text : "";
					var r = new RegExp(array[j], "g");
					pattern = pattern.replace(r, text);
				}
			}
			return pattern;
		}
	};
}

